package com.videoweb.controller;


import com.videoweb.annotation.Anonymous;
import com.videoweb.constant.AuditStatus;
import com.videoweb.entity.VideoComment;
import com.videoweb.entity.dto.VideoCommentDTO;
import com.videoweb.entity.response.AuditResponse;
import com.videoweb.service.IVideoCommentService;
import com.videoweb.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lmr
 * @since 2025-05-17
 */
@Anonymous

@RestController
@RequestMapping("/comment")
public class VideoCommentController {


    @Autowired
    IVideoCommentService videoCommentService;
    //    findCommentByVideoId   根据视频的id来查找评论
//    comment/id
    @GetMapping("/{id}")
    public AjaxResult findVideoByCategoryId(@PathVariable("id") Long videoId) {

        AjaxResult result = AjaxResult.success();
        List<VideoCommentDTO> commentDTOList = videoCommentService.findCommentByVideoId(videoId);
        result.put("comments", commentDTOList);
        return result;
    }

    // 添加和删除评论
    @PostMapping("/add")
    public AjaxResult addComment(@RequestBody VideoComment comment) {
        AjaxResult result;
        AuditResponse auditResponse = videoCommentService.auditComment(comment.getContent());
//        先暂时把他关了，不然要收钱的   [0,1000]   0.036 千次  4分 1000次
         if (auditResponse.getAuditStatus() != AuditStatus.PASS  ) {
            result = AjaxResult.error("评论失败,内容涉及:" + auditResponse.getMsg());
        } else if (videoCommentService.addComment(comment) > 0) {
            result = AjaxResult.success("评论成功");
        } else {
            result = AjaxResult.error("评论失败");
        }

        return result;
    }


    // 添加和删除评论

    @DeleteMapping("/del/{id}")
    public AjaxResult delComment(@PathVariable("id") Long commentId) {

        AjaxResult result;
        if (videoCommentService.delCommentById(commentId) > 0) {
            result = AjaxResult.success("删除评论成功");
        } else {
            result = AjaxResult.error("删除评论失败");
        }

        return result;
    }


}
